package xin.yangda.poiplus.test.service.impl;

import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xin.yangda.poiplus.constants.PoiPlusFileExtend;
import xin.yangda.poiplus.designer.MultipleXlsDesigner;
import xin.yangda.poiplus.pojo.ExcelData;
import xin.yangda.poiplus.test.entity.OrderEntity;
import xin.yangda.poiplus.test.entity.UserEntity;
import xin.yangda.poiplus.test.service.OrderService;
import xin.yangda.poiplus.test.service.UserOrderService;
import xin.yangda.poiplus.test.service.UserService;

import java.io.InputStream;
import java.util.List;

/**
 * @author yangdaxin
 * @version 创建时间 2019/1/22 14:50
 */
@Service
public class UserOrderServiceImpl implements UserOrderService {
    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;

    @Override
    public void exportExcelByTemplate(String tempPath, PoiPlusFileExtend poiPlusFileExtend) {
        // 打开模板
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream(tempPath);
        try (MultipleXlsDesigner multipleXlsDesigner = new MultipleXlsDesigner(inputStream)) {
            // 填充数据源
            ExcelData<UserEntity> userData = new ExcelData<>(UserEntity.class, userService.queryList());
            ExcelData<OrderEntity> orderData = new ExcelData<>(OrderEntity.class, orderService.queryList());

            List<ExcelData> excelDataList = Lists.newArrayList();
            excelDataList.add(userData);
            excelDataList.add(orderData);

            multipleXlsDesigner.setData(excelDataList);

            // 数据加工
            multipleXlsDesigner.process();

            // 保存Excel
            String saveFilePath = "D:\\UserOrderData" + poiPlusFileExtend.getValue();
            multipleXlsDesigner.saveFile(saveFilePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void exportExcel(PoiPlusFileExtend poiPlusFileExtend) {
        try (MultipleXlsDesigner multipleXlsDesigner = new MultipleXlsDesigner()) {
            // 填充数据源
            ExcelData<UserEntity> userData = new ExcelData<>(UserEntity.class, userService.queryList());
            ExcelData<OrderEntity> orderData = new ExcelData<>(OrderEntity.class, orderService.queryList());

            List<ExcelData> excelDataList = Lists.newArrayList();
            excelDataList.add(userData);
            excelDataList.add(orderData);

            multipleXlsDesigner.setData(excelDataList);

            // 数据加工
            multipleXlsDesigner.process();

            // 保存Excel
            String saveFilePath = "D:\\UserOrderData" + poiPlusFileExtend.getValue();
            multipleXlsDesigner.saveFile(saveFilePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void exportTemplate(PoiPlusFileExtend poiPlusFileExtend) {
        try (MultipleXlsDesigner multipleXlsDesigner = new MultipleXlsDesigner()) {
            // 填充数据源
            ExcelData<UserEntity> userData = new ExcelData<>(UserEntity.class);
            ExcelData<OrderEntity> orderData = new ExcelData<>(OrderEntity.class);

            List<ExcelData> excelDataList = Lists.newArrayList();
            excelDataList.add(userData);
            excelDataList.add(orderData);

            multipleXlsDesigner.setData(excelDataList);

            // 模板加工
            multipleXlsDesigner.process(true);

            // 保存Excel
            String saveFilePath = "D:\\UserOrderTemplate" + poiPlusFileExtend.getValue();
            multipleXlsDesigner.saveFile(saveFilePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
